package com.yaozheng.vocationaltraining.service.impl;

import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.IBuyCalculatorView;
import com.yaozheng.vocationaltraining.iview.ICarTypeDetailView;
import com.yaozheng.vocationaltraining.service.CarTypeDetailService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;

/**
 * Created by moon on 2017/11/14.
 */

@EBean
public class CarTypeDetailServiceImpl implements CarTypeDetailService {
    private ICarTypeDetailView iTokenBaseView;
    @Override
    public void init(ICarTypeDetailView iTokenBaseView) {
        this.iTokenBaseView = iTokenBaseView;
    }

    @Background
    @Override
    public void list(int categoryId) {
        try {
            OkHttpClientManager
                    .getInstance()
                    .get(Constants.ROOTURL + "/car/"+categoryId+"/types" ,
                            iTokenBaseView.getToken(), new BaseView(iTokenBaseView) {
                                @Override
                                public void responseSuccess(JSONObject jsonObject) {
                                    iTokenBaseView.responseSuccess(jsonObject);
                                }

                                @Override
                                public void responseError(String error) {
                                    iTokenBaseView.responseError(error);
                                }
                            });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenBaseView.isResponseResult()) {
                iTokenBaseView
                        .responseError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }

    @Background
    @Override
    public void repaymentPeriod() {
        try {
            OkHttpClientManager
                    .getInstance()
                    .get(Constants.ROOTURL + "/car/interest-rates" ,
                            iTokenBaseView.getToken(), new BaseView(iTokenBaseView) {
                                @Override
                                public void responseSuccess(JSONObject jsonObject) {
                                    iTokenBaseView.repaymentPeriodSuccess(jsonObject);
                                }

                                @Override
                                public void responseError(String error) {
                                    iTokenBaseView.repaymentPeriodError(error);
                                }
                            });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenBaseView.isResponseResult()) {
                iTokenBaseView
                        .repaymentPeriodError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }

    @Background
    @Override
    public void downPaymentRatios() {
        try {
            OkHttpClientManager
                    .getInstance()
                    .get(Constants.ROOTURL + "/car/down-payment-ratios" ,
                            iTokenBaseView.getToken(), new BaseView(iTokenBaseView) {
                                @Override
                                public void responseSuccess(JSONObject jsonObject) {
                                    iTokenBaseView.downPaymentSuccess(jsonObject);
                                }

                                @Override
                                public void responseError(String error) {
                                    iTokenBaseView.downPaymentError(error);
                                }
                            });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenBaseView.isResponseResult()) {
                iTokenBaseView
                        .downPaymentError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }

}
